<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProclamationController extends Controller
{
	//首页显示
	public function getIndex(Request $request)
	{

		if($request['name']){
            //搜索分页
            $res = DB::table('proclamation')
            ->where('ProclamationClass','like','%'.$request['name'].'%')
            ->paginate(10);
        }else{
            //所有分页
            $res = DB::table('proclamation')
            ->paginate(3);
        }
       
        $info = $request -> all();
        // dd($res);

		return view('Lzk.Proclamation.ProclamationClass',['res'=>$res,'info'=>$info]);
	}
	//解析添加页面
	public function getAdd()
	{
		return view('Lzk.Proclamation.ProclamationAdd');
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$data = $request->only('ProclamationClass','ProclamationExplain');
		$res = DB::table('proclamation')->insert($data);
		if($res){
			return redirect('/Proclamation');
		}else{
			return back();
		}
	}

	//解析修改页面
	public function getEdit($id)
	{
		$ids = DB::table('proclamation')->where('id',$id)->first();
		return view('Lzk.Proclamation.ProclamationEdit',['ids'=>$ids]);
	}	

	//执行修改
	public function postUpdate(Request $request)
	{
		
		// dd($request->all());
		$id = $request->only('id');
		// dd($id);
		$update  = $request->only('ProclamationClass','ProclamationExplain');

		$ress = DB::table('proclamation')->where('id',$id)->update($update);                  
		if($ress){
			return redirect('/Proclamation');
		}else{
			return back();
		}                                                                                                                                                         

	}

	//删除
	public function getDel($id)
	{

		$del = DB::select("select ProclamationId from notice where ProclamationId = {$id}");
		// dd($a);
        if($del){
        	echo '<script>alert("类别下有公告，不能删除此类别");window.location.href="/Proclamation";</script>';
        }else{
        	DB::table('Proclamation')->delete($id);
        	return redirect('/Proclamation');
        }

	}

	//详情页面
	public function getLook($id)
	{
		$looks = DB::table('notice')
            ->leftJoin('proclamation', 'notice.ProclamationId', '=', 'proclamation.id')
            ->where('notice.ProclamationId', '=' ,$id)
            ->get();
            // dd($notice);


		return view('Lzk.Proclamation.ProclamationDetails',['looks'=>$looks]);

	}

	//查看页
	public function getLookk($id)
	{
		
		$lookk = DB::table('notice')->where('NoticeID',$id)->first();
		// dd($lookk);
		return view('Lzk.Proclamation.ProclamationShowDetail',['lookk'=>$lookk]);
	}

}
